import { DirectiveBinding } from 'vue';
import { useUserStore } from '@/store';
// 检测当前权限名称是否在用户permissions中
function checkPermission(el: HTMLElement, binding: DirectiveBinding) {
  const { value } = binding;
  const userStore = useUserStore();
  const { permissions, admin } = userStore;

  if (value && !admin) {
    const hasPermission = permissions?.includes(value);
    if (!hasPermission && el.parentNode) {
      el.parentNode.removeChild(el);
    }
  }
  // else {
  //   throw new Error(`need roles! Like v-permission="'admin'"`);
  // }
}

export default {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    checkPermission(el, binding);
  },
  updated(el: HTMLElement, binding: DirectiveBinding) {
    checkPermission(el, binding);
  },
};
